//重写数组

//获取原来的数组方法
let oldArrayProtoMethods = Array.prototype;

//继承方法
export let ArrMethods = Object.create(oldArrayProtoMethods);

//劫持方法
let methods = ["push", "pop", "unshift", "shift", "splice"];

methods.forEach((item) => {
  ArrMethods[item] = function (...args) {
    let ob = this.__ob__; //value.push 所以this指向的是data里面的数组值
    let result = oldArrayProtoMethods[item].apply(this, args);
    //数组追加分情况处理
    let inserted;
    switch (item) {
      case "push":
        inserted = args;
        break;
      case "unshift":
        inserted = args;
        break;
      case "splice":
        inserted = args.splice(2); //arrgs.splice(0,1,{b:5})
        break;
      default:
        break;
    }
   
    if (inserted) {
      ob.observerArray(inserted);
    }
    ob.dep.notify();
    return result;
  };
});
